//
//  BDFindPwdTableViewController.m
//  BeidouMall
//
//  Created by qie on 16/4/1.
//  Copyright © 2016年 qie. All rights reserved.
//

#import "BDFindPwdTableViewController.h"

@interface BDFindPwdTableViewController ()
{
    NSString *_title;
}
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UITextField *passwrodTextField;
@property (weak, nonatomic) IBOutlet UITextField *secondpasswrodTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;

@end

@implementation BDFindPwdTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if ([self.userInfo.allKeys containsObject:@"authorization"]) {
        self.title = @"找回密码";
    } else {
        self.title = @"修改密码";
    }
    _title = self.title;
    
    self.bottomView.height = kScreen_Height - 230;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - TableView delegate

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

#pragma mark - TextField delegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString *futrueString = [textField.text changeCharactersInRange:range replacementString:string];
    
    if ([_passwrodTextField.text length]
        && [_secondpasswrodTextField.text length]) {
        _loginButton.enabled = YES;
    } else {
        _loginButton.enabled = NO;
    }
    
    if (textField.tag == 101) {
        if (futrueString.length > kPhoneMaxLength) {
            return NO;
        }
    } else if (textField.tag == 102) {
        if (futrueString.length > kPasswordMaxLength) {
            return NO;
        }
    }
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if (textField.tag == 101) {
        [_secondpasswrodTextField becomeFirstResponder];
    } else if (textField.tag == 102) {
        
    }
    return YES;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (textField.tag == 101) {
        _passwrodTextField.text = nil;
        _secondpasswrodTextField.text = nil;
    } else if (textField.tag == 102) {
        if (_secondpasswrodTextField.text.length > 0) {
            _secondpasswrodTextField.text = nil;
        }
    }
    return YES;
}

#pragma mark - scrollView delegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (_passwrodTextField.isFirstResponder) {
        [_passwrodTextField resignFirstResponder];
    }else if (_secondpasswrodTextField.isFirstResponder) {
        [_secondpasswrodTextField resignFirstResponder];
    }
}

#pragma mark - IBActions

- (IBAction)switchButtonPressed:(UIButton *)sender {
    sender.selected = !sender.selected;
    _passwrodTextField.secureTextEntry = !sender.selected;
    _secondpasswrodTextField.secureTextEntry = !sender.selected;
}

- (IBAction)loginButtonPressed:(UIButton *)sender {
    if (_passwrodTextField.text.length == 0) {
        [MBProgressHUD showToast:self.view withText:@"请输入密码"];
    } else if (![_passwrodTextField.text isValidPassword]) {
        [MBProgressHUD showToast:self.view withText:@"密码格式不正确"];
    } else if (_secondpasswrodTextField.text.length == 0) {
        [MBProgressHUD showToast:self.view withText:@"请输入确认密码"];
    } else if (![_passwrodTextField.text isEqualToString:_secondpasswrodTextField.text]) {
        [MBProgressHUD showToast:self.view withText:@"两次输入的密码不一致"];
    } else {
        if (_passwrodTextField.isFirstResponder) {
            [_passwrodTextField resignFirstResponder];
        } else if (_secondpasswrodTextField.isFirstResponder) {
            [_secondpasswrodTextField resignFirstResponder];
        }
        NSString *url = [NSString stringWithFormat:@"%@%@",[BDNetworking shareInstance].baseUrl,Modify_Pwd];
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view withText:@"正在找回密码"];
        
        NSString *userId = self.userInfo[@"user"][@"userId"];
        
        [[BDAccountManagerModel shareInstance] modifyPwdWIthUrl:url userId:userId password:_passwrodTextField.text completionHandler:^(NSError *error) {
            if (error != nil) {
                NSLog(@"%@失败%@", _title,[error localizedDescription]);
                hud.labelText = [error localizedDescription];
                [hud hideAfterDelay:0.5];
            } else {
                
                hud.labelText = [NSString stringWithFormat:@"%@成功",_title];
                [hud hideAfterDelay:0.5];
                
//                [[BDAccountManagerModel shareInstance] saveNormarlUserInformation:self.userInfo];
                
                dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC);
                dispatch_after(time, dispatch_get_main_queue(), ^{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                });
            }
        }];
    }
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
